package com.cskaoyan.service;

import com.cskaoyan.bean.System;
import com.cskaoyan.bean.SystemExample;
import com.cskaoyan.bean.config.ExpressConfig;
import com.cskaoyan.bean.config.MallConfig;
import com.cskaoyan.bean.config.OrderConfig;
import com.cskaoyan.bean.config.WxConfig;
import com.cskaoyan.mapper.SystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: heliming
 * @date: 2021/6/5
 * @description:
 */
@Service
public class AdminConfigServiceImpl implements AdminConfigService {
@Autowired
    SystemMapper systemMapper;

    @Override
    public MallConfig selectMall() {
        MallConfig mallConfig = new MallConfig();
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("%cskaoyan_mall_mall%");

        List<System> systemList = systemMapper.selectByExample(systemExample);
        for (System system : systemList) {
            if (system.getKeyName().endsWith("name")) {
                mallConfig.setCskaoyanmall_mall_name(system.getKeyValue());
            } else if (system.getKeyName().endsWith("address")) {
                mallConfig.setCskaoyanmall_mall_address(system.getKeyValue());
            } else if (system.getKeyName().endsWith("phone")) {
                mallConfig.setCskaoyanmall_mall_phone(system.getKeyValue());
            } else if (system.getKeyName().endsWith("qq")) {
                mallConfig.setCskaoyanmall_mall_qq(system.getKeyValue());
            }
        }

        return mallConfig;
    }

    @Override
    public void updateMall(MallConfig mallConfig) {
        System system = new System();
        system.setKeyValue(mallConfig.getCskaoyanmall_mall_name());
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameEqualTo("cskaoyan_mall_mall_name");
        systemMapper.updateByExampleSelective(system, systemExample);

        System system2 = new System();
        system2.setKeyValue(mallConfig.getCskaoyanmall_mall_address());
        SystemExample systemExample2 = new SystemExample();
        SystemExample.Criteria criteria2 = systemExample2.createCriteria();
        criteria2.andKeyNameEqualTo("cskaoyan_mall_mall_address");
        systemMapper.updateByExampleSelective(system2, systemExample2);


        System system3 = new System();
        system3.setKeyValue(mallConfig.getCskaoyanmall_mall_phone());
        SystemExample systemExample3 = new SystemExample();
        SystemExample.Criteria criteria3 = systemExample3.createCriteria();
        criteria3.andKeyNameEqualTo("cskaoyan_mall_mall_phone");
        systemMapper.updateByExampleSelective(system3, systemExample3);

        System system4 = new System();
        system4.setKeyValue(mallConfig.getCskaoyanmall_mall_qq());
        SystemExample systemExample4 = new SystemExample();
        SystemExample.Criteria criteria4 = systemExample4.createCriteria();
        criteria4.andKeyNameEqualTo("cskaoyan_mall_mall_qq");
        systemMapper.updateByExampleSelective(system4, systemExample4);

    }

    @Override
    public ExpressConfig selectExpress() {
        ExpressConfig expressConfig = new ExpressConfig();
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("%cskaoyan_mall_express_freight%");

        List<System> systemList = systemMapper.selectByExample(systemExample);
        for (System system : systemList) {
            if (system.getKeyName().endsWith("min")) {
                expressConfig.setCskaoyanmall_express_freight_min(system.getKeyValue());
            } else if (system.getKeyName().endsWith("value")) {
                expressConfig.setCskaoyanmall_express_freight_value(system.getKeyValue());
            }
        }

        return expressConfig;
    }

    @Override
    public void updateExpress(ExpressConfig expressConfig) {
        System system1 = new System();
        system1.setKeyValue(expressConfig.getCskaoyanmall_express_freight_min());
        SystemExample systemExample1 = new SystemExample();
        SystemExample.Criteria criteria1 = systemExample1.createCriteria();
        criteria1.andKeyNameEqualTo("cskaoyan_mall_express_freight_min");
        systemMapper.updateByExampleSelective(system1, systemExample1);

        System system2 = new System();
        system2.setKeyValue(expressConfig.getCskaoyanmall_express_freight_value());
        SystemExample systemExample2 = new SystemExample();
        SystemExample.Criteria criteria2 = systemExample2.createCriteria();
        criteria2.andKeyNameEqualTo("cskaoyan_mall_express_freight_value");
        systemMapper.updateByExampleSelective(system2, systemExample2);

    }

    @Override
    public OrderConfig selectOrder() {
        OrderConfig orderConfig = new OrderConfig();
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("%cskaoyan_mall_order%");

        List<System> systemList = systemMapper.selectByExample(systemExample);
        for (System system : systemList) {
            if (system.getKeyName().endsWith("comment")) {
                orderConfig.setCskaoyanmall_order_comment(system.getKeyValue());
            } else if (system.getKeyName().endsWith("unconfirm")) {
                orderConfig.setCskaoyanmall_order_unconfirm(system.getKeyValue());
            } else if (system.getKeyName().endsWith("unpaid")) {
                orderConfig.setCskaoyanmall_order_unpaid(system.getKeyValue());
            }
        }
        return orderConfig;
    }

    @Override
    public void updateOrder(OrderConfig orderConfig) {
        System system1 = new System();
        system1.setKeyValue(orderConfig.getCskaoyanmall_order_comment());
        SystemExample systemExample1 = new SystemExample();
        SystemExample.Criteria criteria1 = systemExample1.createCriteria();
        criteria1.andKeyNameEqualTo("cskaoyan_mall_order_comment");
        systemMapper.updateByExampleSelective(system1, systemExample1);


        System system2 = new System();
        system2.setKeyValue(orderConfig.getCskaoyanmall_order_unconfirm());
        SystemExample systemExample2 = new SystemExample();
        SystemExample.Criteria criteria2 = systemExample2.createCriteria();
        criteria2.andKeyNameEqualTo("cskaoyan_mall_order_unconfirm");
        systemMapper.updateByExampleSelective(system2, systemExample2);

        System system3 = new System();
        system3.setKeyValue(orderConfig.getCskaoyanmall_order_unpaid());
        SystemExample systemExample3 = new SystemExample();
        SystemExample.Criteria criteria3 = systemExample3.createCriteria();
        criteria3.andKeyNameEqualTo("cskaoyan_mall_order_unpaid");
        systemMapper.updateByExampleSelective(system3, systemExample3);
    }

    @Override
    public WxConfig selectWx() {
        WxConfig wxConfig = new WxConfig();
        SystemExample systemExample = new SystemExample();
        SystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("%cskaoyan_mall_wx%");

        List<System> systemList = systemMapper.selectByExample(systemExample);
        for (System system : systemList) {
            if (system.getKeyName().endsWith("goods")) {
                wxConfig.setCskaoyanmall_wx_catlog_goods(system.getKeyValue());
            } else if (system.getKeyName().endsWith("list")) {
                wxConfig.setCskaoyanmall_wx_catlog_list(system.getKeyValue());
            } else if (system.getKeyName().endsWith("brand")) {
                wxConfig.setCskaoyanmall_wx_index_brand(system.getKeyValue());
            } else if (system.getKeyName().endsWith("hot")) {
                wxConfig.setCskaoyanmall_wx_index_hot(system.getKeyValue());
            } else if (system.getKeyName().endsWith("new")) {
                wxConfig.setCskaoyanmall_wx_index_new(system.getKeyValue());
            } else if (system.getKeyName().endsWith("topic")) {
                wxConfig.setCskaoyanmall_wx_index_topic(system.getKeyValue());
            } else if (system.getKeyName().endsWith("share")) {
                wxConfig.setCskaoyanmall_wx_share(system.getKeyValue());
            }
        }
        return wxConfig;
    }

    @Override
    public void updateWx(WxConfig wxConfig) {
        System system1 = new System();
        system1.setKeyValue(wxConfig.getCskaoyanmall_wx_catlog_goods());
        SystemExample systemExample1 = new SystemExample();
        SystemExample.Criteria criteria1 = systemExample1.createCriteria();
        criteria1.andKeyNameEqualTo("cskaoyan_mall_wx_catlog_goods");
        systemMapper.updateByExampleSelective(system1, systemExample1);

        System system2 = new System();
        system2.setKeyValue(wxConfig.getCskaoyanmall_wx_catlog_list());
        SystemExample systemExample2 = new SystemExample();
        SystemExample.Criteria criteria2 = systemExample2.createCriteria();
        criteria2.andKeyNameEqualTo("cskaoyan_mall_wx_catlog_list");
        systemMapper.updateByExampleSelective(system2, systemExample2);

        System system3 = new System();
        system3.setKeyValue(wxConfig.getCskaoyanmall_wx_index_brand());
        SystemExample systemExample3 = new SystemExample();
        SystemExample.Criteria criteria3 = systemExample3.createCriteria();
        criteria3.andKeyNameEqualTo("cskaoyan_mall_wx_index_brand");
        systemMapper.updateByExampleSelective(system3, systemExample3);

        System system4 = new System();
        system4.setKeyValue(wxConfig.getCskaoyanmall_wx_index_hot());
        SystemExample systemExample4 = new SystemExample();
        SystemExample.Criteria criteria4 = systemExample4.createCriteria();
        criteria4.andKeyNameEqualTo("cskaoyan_mall_wx_index_hot");
        systemMapper.updateByExampleSelective(system4, systemExample4);


        System system5 = new System();
        system5.setKeyValue(wxConfig.getCskaoyanmall_wx_index_new());
        SystemExample systemExample5 = new SystemExample();
        SystemExample.Criteria criteria5 = systemExample5.createCriteria();
        criteria5.andKeyNameEqualTo("cskaoyan_mall_wx_index_new");
        systemMapper.updateByExampleSelective(system5, systemExample5);

        System system6 = new System();
        system6.setKeyValue(wxConfig.getCskaoyanmall_wx_index_topic());
        SystemExample systemExample6 = new SystemExample();
        SystemExample.Criteria criteria6 = systemExample6.createCriteria();
        criteria6.andKeyNameEqualTo("cskaoyan_mall_wx_index_topic");
        systemMapper.updateByExampleSelective(system6, systemExample6);


        System system7 = new System();
        system7.setKeyValue(wxConfig.getCskaoyanmall_wx_share());
        SystemExample systemExample7 = new SystemExample();
        SystemExample.Criteria criteria7 = systemExample7.createCriteria();
        criteria7.andKeyNameEqualTo("cskaoyan_mall_wx_share");
        systemMapper.updateByExampleSelective(system7, systemExample7);
    }
}
